package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.ScenicCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.query.ScenicReviewsQuery;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collections;
import java.util.List;
import java.util.Optional;


@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private ScenicCommentMongoRepository scenicCommentMongoRepository;

    @Autowired
    private MongoTemplate template;

    /**
     * 评论状态
     * 2星以下差评 category=1
     * 3星中评 category=2
     * 4/5星好评 category=3
     * 带图片的 category=4
     **/
    @Override
    public void commentAdd(ScenicComment comment) {
        int rank = comment.getRank();//星星
        String tupianUrl = comment.getTupianUrl();//图片地址
        if (rank <= 2) {
            comment.setCategory(1);//2星以下差评  评论的状态
        } else if (rank == 3) {
            comment.setCategory(2);//3星中评
        } else {
            comment.setCategory(3);//4/5星好评
        }
        //如果是评论下回复
        if (ScenicComment.SCENIC_COMMENT_TYPE.equals(comment.getType())) { //传进来的评论类型和实体类的评论类型作比较
            Optional<ScenicComment> ops = scenicCommentMongoRepository.findById(comment.getId());//获取评论的id
            //使用isPresent()方法判断有无，有则返回，无则返回null
            if (ops.isPresent()) {//是否存在
                ScenicComment scenicComment = ops.get();
                scenicComment.getComments().add(comment);
                scenicCommentMongoRepository.save(scenicComment);
            }
        } else {
            //如果是评论
            scenicCommentMongoRepository.save(comment);
        }
    }

    /**
     * 获取景点真实评论 数
     */
    @Override
    public int getCommentCount(Long id) {
        return scenicCommentMongoRepository.countByScenicId(id);
    }

    /**
     * 获取所有评论
     *
     * @param qo
     * @param scenicId
     * @return
     */
    @Override
    public Page<ScenicComment> getComment(ScenicReviewsQuery qo, Long scenicId) {
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        return scenicCommentMongoRepository.findByScenicId(scenicId, pageable);
    }

    /**
     * 评论类型 1差 2中 3好
     *
     * @param category
     * @param id
     * @return
     */
    @Override
    public Integer getCountByCategory(Integer category, Long id) {
        return scenicCommentMongoRepository.countByCategoryAndScenicId(category, id);
    }

    /**
     * 点赞
     *
     * @param id
     * @param userId
     * @return 1:点赞 0:取消点赞
     */
    @Override
    public Integer strategyThumbup(String id, Long userId) {
        ScenicComment scenicComment = scenicCommentMongoRepository.findById(id).get();
        //获取点赞集合
        List<Long> thumbup = scenicComment.getThumbup();
        //设置一个state 区分点赞还是取消点赞
        int state = 0;  //默认取消点赞
        if (thumbup.contains(userId)) {
            //包含 -> 取消
            thumbup.remove(userId);
            //点赞数-1
            scenicComment.setThumbupnum(scenicComment.getThumbupnum() - 1);
        } else {
            //不包含 -> 点赞
            thumbup.add(userId);
            //点赞数+1
            scenicComment.setThumbupnum(scenicComment.getThumbupnum() + 1);
            //将标记设置为1
            state = 1;
        }
        //将数据同步到mongodb
        this.commentAdd(scenicComment);
        return state;
    }

    @Override
    public List<ScenicComment> getByUserId(Long uid) {
        if (uid == null) {
            return Collections.emptyList();
        }
        Query query = new Query();
        query.with(Sort.by(Sort.Direction.ASC, "createTime"));
        query.addCriteria(Criteria.where("userId").is(uid));
        List<ScenicComment> list = template.find(query, ScenicComment.class);
        return list;
    }

    @Override
    public Page<ScenicComment> getCommentBtCategory(ScenicReviewsQuery qo) {
        Pageable pageable= PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        //分类
        Integer category = qo.getCategory();
        //景点
        Long scenicId = qo.getScenicId();
        Page<ScenicComment> page = null;
        //区分评论类型
        if(category == 0 || category == 4){
            page = scenicCommentMongoRepository.findByScenicId(scenicId, pageable);
        }else{
            page = scenicCommentMongoRepository.findByCategoryAndScenicId(category,scenicId ,pageable );
        }

        return page;
    }

}
